﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace BYS.XiAnComDept.BussLogic
{
    public class Fund : IDataErrorInfo
    {
        ValidateEntity<Fund> validator = new ValidateEntity<Fund>();

        public Fund()
        {
            this.FundGuid = Guid.NewGuid();
            this.ProjectFields = new ObservableCollection<ProjectField>();
        }

        [Key]
        [Column(Order = 1)]
        public Guid FundGuid { get; set; }

        [Column(Order = 2)]
        [Display(Name = "年度")]
        [DisplayFormat(DataFormatString ="yyyy")]
        [Required(ErrorMessage="年度必须输入！")]
        public DateTime Year { get; set; }

        [Required(ErrorMessage = "单位必须输入！")]
        public Company Company { get; set; }

        [Required(ErrorMessage="项目类型必须输入！")]
        public FundType FundType { get; set; }

        [Required(ErrorMessage = "状态必须输入！")]
        [MaxLength(10, ErrorMessage="Must Less than 10 chars.")]
        public string FundStatus { get; set; }

        [Required(ErrorMessage = "申报金额必须输入！")]
        [DataType(DataType.Currency, ErrorMessage = "申报金额必须是数字！")]
        [DisplayFormat(DataFormatString="N2")]
        public decimal Amount { get; set; }

        [Required(ErrorMessage = "审核金额必须输入！")]
        [DataType(DataType.Currency, ErrorMessage = "审核金额必须是数字！")]
        [DisplayFormat(DataFormatString = "N2")]
        public decimal ApprovedAmount { get; set; }

        [Required(ErrorMessage = "尾款金额必须输入！")]
        [DataType(DataType.Currency, ErrorMessage="尾款金额必须是数字！")]
        [Display(Name="尾款")]
        [DisplayFormat(DataFormatString = "N2")]
        public decimal DueAmount { get; set; }

        [Display(Name="备注")]
        [MaxLength(2000, ErrorMessage="备注不能超过2000个字！")]
        public string Comment { get; set; }

        public ObservableCollection<ProjectField> ProjectFields { get; set; }

        [NotMapped]
        public string this[string projectFieldName]
        {
            get
            {
                ProjectField field = this.ProjectFields.SingleOrDefault(x => x.FieldName.Trim() == projectFieldName.Trim());
                if (field == null)
                {
                    return string.Empty;
                }
                else
                {
                    return field.FieldValue;
                }
            }
        }

        [NotMapped]
        public string ProjectInfo 
        { 
            get 
            {
                return string.Join(", ", ProjectFields.OrderBy(x => x.OrderNo));
            }
        }

        public bool HasSameProjectFields(IEnumerable<ProjectField> projectFields)
        {
            if (this.ProjectFields.Count == projectFields.Count())
            {
                foreach (ProjectField myField in this.ProjectFields)
                {
                    int matchedCount = projectFields.Count(x => x.FieldName == myField.FieldName && x.FieldValue == myField.FieldValue);
                    if (matchedCount == 0)
                    {
                        return false;
                    }
                }
                return true;
            }
            else
            {
                return false;
            }
        }

        [NotMapped]
        string IDataErrorInfo.Error
        {
            get {  throw new NotImplementedException(); }
        }

        [NotMapped]
        string IDataErrorInfo.this[string columnName]
        {
            get 
            {
                if (columnName == "Year")
                {
                    if (this.Year.Year < 2000)
                    {
                        return "年度必须输入！";
                    }
                }
                return validator.ValidateProperty(this, columnName);
            }
        }
    }

   
}
